package com.citycloud.ccuap.tc.yq.metafield;

import java.util.List;

import com.citycloud.ccuap.tc.yq.constant.YqConstant;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.SelectProvider;
import org.springframework.cache.annotation.Cacheable;

import com.citycloud.ccuap.commons.db.SqlRestrictions;
import com.citycloud.ccuap.tc.yq.dto.FieldDTO;
import com.citycloud.ccuap.tc.yq.entity.MetaField;


@Mapper
public interface MetaFieldMapper {
	@SelectProvider(type = SqlProvider.class, method = "getUserFields")
	@Cacheable(cacheNames="userFieldsCache", key = "#p0 + '_' + #p1")
	List<FieldDTO> getUserFields(String userId, String projectId);
	
	@SelectProvider(type = SqlProvider.class, method = "getDataGroupFields")
	@Cacheable(cacheNames="dataGroupFieldsCache", key = "#p0")
	List<FieldDTO> getDataGroupFields(String datagroupId);
	
	@SelectProvider(type = SqlProvider.class, method = "getDataGroup")
	@Cacheable(cacheNames="dataGroupCache", key = "#p0 + '_' + #p1")
	@ResultType(value = String.class)
	List<String> getDataGroup(String userId, String projectId);
	
	@SelectProvider(type = SqlProvider.class, method = "getFieldByTableConfigId")
	@Cacheable(cacheNames="tableFieldsCache", key = "#p0")
	List<FieldDTO> getFieldByTableConfigId(String tableConfigId);
	
	@SelectProvider(type = SqlProvider.class, method = "getFieldsByTableIdAndEnamesAndBusiTypeNotNull")
	@ResultMap("com.citycloud.ccuap.tc.yq.entity.MetaField.ResultMapExt")
	List<MetaField> getFieldsByTableIdAndEnamesAndBusiTypeNotNull(String tableId);
	
	@SelectProvider(type = SqlProvider.class, method = "getFieldCnameByTableIdAndFieldEnames")
    String getFieldCnameByTableIdAndFieldEnames(String tableId,List<String> fieldEnames);
	
	class SqlProvider {
		
		public String getFieldCnameByTableIdAndFieldEnames(String tableId,List<String> enames) {
			StringBuffer sql = new StringBuffer("select group_concat(t.CNAME) from ap_t_meta_field_view t where 1=1  ");
			sql.append(SqlRestrictions.eq("t.TABLE_ID", tableId));
			sql.append(SqlRestrictions.in("t.ENAME", enames.toArray()));
			return sql.toString();
		}
		
		public String getFieldsByTableIdAndEnamesAndBusiTypeNotNull(String tableId) {
			StringBuffer sql = new StringBuffer("SELECT  atm_f.* FROM  ap_t_meta_field_view AS atm_f WHERE   atm_f.BUS_TYPE is not null ");
            sql.append(SqlRestrictions.eq("atm_f.TABLE_ID", tableId));
			return sql.toString();
		}
		
		public String getByTableId(String tableId) {
			StringBuffer sql = new StringBuffer("SELECT  atm_f.* FROM  ap_t_meta_field_view AS atm_f WHERE 1 = 1");
			sql.append(SqlRestrictions.eq("atm_f.TABLE_ID", tableId));
			return sql.toString();
		}
		public String getAllBydataGroupId(String groupId,String datasource) {
			StringBuffer sql = new StringBuffer("SELECT atm_f.* FROM ap_t_meta_field_view AS atm_f LEFT JOIN " +
					"sys_dimension_field AS ats_df ON atm_f.OBJ_ID = ats_df.field_id LEFT JOIN " +
					"ap_t_meta_table_view AS atm_t ON atm_f.TABLE_ID = atm_t.OBJ_ID  WHERE 1 = 1");
			sql.append(SqlRestrictions.eq("ats_df.DATAGROUP_ID", groupId));
			if (!YqConstant.STR_NULL.equals(datasource) && StringUtils.isNotBlank(datasource)) {
				sql.append(SqlRestrictions.eq("atm_t.DB_ID", datasource));
			}
			return sql.toString();
		}
		
		public String getAllBydataGroupIdNew(String groupId,String datasource,String tableId) {
			StringBuffer sql = new StringBuffer("SELECT atm_f.* FROM ap_t_meta_field_view AS atm_f inner JOIN " +
					"sys_dimension_field AS ats_df ON atm_f.OBJ_ID = ats_df.field_id inner JOIN " +
					"ap_t_meta_table_view AS atm_t ON atm_f.TABLE_ID = atm_t.OBJ_ID  WHERE 1 = 1");
			sql.append(SqlRestrictions.eq("ats_df.DATAGROUP_ID", groupId));
			if (!YqConstant.STR_NULL.equals(datasource) && StringUtils.isNotBlank(datasource)) {
				sql.append(SqlRestrictions.eq("atm_t.DB_ID", datasource));
			}
			sql.append(SqlRestrictions.eq("atm_f.TABLE_ID", tableId));
			
			return sql.toString();
		}

		
		public String getUserFields(String userId, String projectId) {
			String  sql="select distinct ut.db_id as dbId,ut.table_id as tableId,ut.ename,fv.bus_type as busType "
					   +"from v_user_tablefields ut,ap_t_meta_field_view fv "
					   + "where ut.table_id=fv.table_id and ut.ename=fv.ename ";
			sql+=SqlRestrictions.eq("ut.user_id", userId);
			sql+=SqlRestrictions.eq("ut.project_id", projectId);

			return sql;
		};
		
		public String getDataGroupFields(String datagroupId) {
			String  sql="select distinct ut.db_id as dbId,ut.table_id as tableId,ut.ename,fv.bus_type as busType "
					   +"from v_datagroup_tablefields ut,ap_t_meta_field_view fv "
					   + "where ut.table_id=fv.table_id and ut.ename=fv.ename ";
			sql+=SqlRestrictions.eq("ut.datagroup_id", datagroupId);

			return sql;
		};
		
		public String getDataGroup(String userId, String projectId) {
			String sql="SELECT 	DISTINCT sd.datagroup_id FROM sys_act sa JOIN sys_role sr ON sa.role_id = sr.role_id AND sr.project_id IS NOT NULL "
					+ "AND sr.data_status = 1 JOIN sys_project_dg_role_rel sad ON sad.role_id = sr.role_id "
					+ "JOIN sys_datagroup sd ON sad.datagroup_id = sd.datagroup_id AND sd.is_enable = 1 "
					+ "JOIN sys_project_dg_rel spdr ON spdr.datagroup_id = sd.datagroup_id AND "
					+ "spdr.project_id = sr.project_id WHERE 1=1";
			sql+=SqlRestrictions.eq("sa.user_id", userId);
			sql+=SqlRestrictions.eq("sr.project_id", projectId);
			return sql;
		};
		
		public String getFieldByTableConfigId(String tableConfigId) {
			String sql="select distinct fv.table_id as tableId,fv.ename,fv.bus_type as busType  from sys_dimension_field_config f inner join ap_t_meta_field_view fv on f.field_id=fv.OBJ_ID where 1=1";
			sql+=SqlRestrictions.eq("f.table_config_id", tableConfigId);
			return sql;
		};

	}



}
